<?php

require_once("includes/connect.php");
require_once("includes/function.php");

$cheque_no = $_REQUEST["cheque_no"];
$tblname = $_REQUEST["tblname"];

//echo $petition_id;

$db = new myDBS;

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT tbl_pay.cheque_no , bank.bank_name, bank_branch.branch_name, "
                . " credit_name AS receiver, tbl_pay.amount AS net , cheque_app_date"
                . " FROM tbl_creditor, tbl_pet, bank, bank_branch,tbl_pay "
                . " WHERE tbl_pet.status = 'จัดทำเช็ค' "
                . " AND tbl_pay.cheque_no ='$cheque_no'"
                . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
                . " AND tbl_pet.pet_id = tbl_pay.pet_id "
                . " AND tbl_pay.bank_id = bank.bank_id "
                . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
                . " ORDER BY cheque_no , bank_name, branch_name, credit_name";

//        $sql = "SELECT cheque_no, branch_name, bank_name, receiver, cheque_app_date, SUM( net ) AS net "
//                . " FROM tbl_pet "
//                . " WHERE  cheque_no ='$cheque_no' "
//                . " GROUP BY cheque_no, branch_name, bank_name, receiver, cheque_app_date";

        break;

    case "tbl_petition":
        $sql = "SELECT cheque_no, branch_name, bank_name, company_name AS receiver, cheque_app_date, SUM( inpNet) AS net "
                . " FROM tbl_petition "
                . " WHERE  cheque_no ='$cheque_no' "
                . " GROUP BY cheque_no, branch_name, bank_name, receiver, cheque_app_date";

        break;

    case "tbl_salary":
        $sql = "SELECT cheque_no, branch_name, bank_name, receiver, cheque_app_date, SUM( salary) AS net "
                . " FROM tbl_salary "
                . " WHERE  cheque_no ='$cheque_no' "
                . " GROUP BY cheque_no, branch_name, bank_name, receiver, cheque_app_date";

        break;

    case "tbl_welfare":
        $sql = "SELECT cheque_no, branch_name, bank_name, receiver, cheque_app_date, SUM( welfare) AS net "
                . " FROM tbl_welfare "
                . " WHERE  cheque_no ='$cheque_no' "
                . " GROUP BY cheque_no, branch_name, bank_name, receiver, cheque_app_date";

        break;

    default:
        break;
}


$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

$C = new Currency;


// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile('form/cash.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
//Add a custom size  

$pagelayout = array(100, 160);
$pdf = new PDF($orientation = 'L', $unit = 'mm', $pagelayout, $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);


// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);
// now write some text above the imported page
//$pdf->SetXY(22, 8);
//$pdf->Write(0, $sql);


$pdf->SetXY(22, 8);
$pdf->Write(0, $rows->bank_name);

$pdf->SetXY(75, 8);
$pdf->Write(0, $rows->branch_name);

$pdf->SetXY(125, 8);
$pdf->Write(0, $rows->cheque_no);

$pdf->SetXY(110, 18);

if ($rows->cheque_app_date <> "0000-00-00") {
    $date = date_create($rows->cheque_app_date);
    $pdf->Write(0, date_format($date, "d/m/Y"));
}

$pdf->SetXY(20, 30.5);
$pdf->Write(0, $rows->receiver);

//$pdf->SetXY(87, 30.5);
////$pdf->Write(0, "ทวีศักดิ์");
//$html = "<del>XX</del>";
//$pdf->writeHTML($html, true, false, true, false, '');
//
//$style2 = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
//$pdf->Line(87.5, 37, 92.5, 37, $style2);
//
//$pdf->SetXY(85.5, 37);
//$pdf->Write(0, "100");

$pdf->SetXY(25, 41.5);
$pdf->Write(0, number_format($rows->net, 2));


$pdf->SetXY(8, 52);
$pdf->Write(0, $C->bahtThai($rows->net));


//$pdf->Write(0, "asdfasdf");

/*
  $pdf->SetXY(73, 69);
  $pdf->Cell(41,0,number_format($rows->inpValues, 2),0,0,'R');
 */

$pdf->Output('output_cash.pdf', 'I');
?>